// U选商家管理后台
const Controller = require('egg').Controller;
const sqlConfig = require('../util/sqlConfig')
const err = require('../util/err')
const util = require('../util/util')

class UBusinessManageController extends Controller {
    //  获取校园管理员列表  
    async uSchoolManageList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.uSchoolManageList(r_body))
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let data2 = await mysql.select('uSchool', {
                where: { id: element.schoolId}
            })
            element.schoolName = util.handelData(data2)[0].name
            element.key = element.manageId
        }
        const total = util.handelData(await mysql.query(sqlConfig.uSchoolManageTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }
    // 新增校园管理员
    async uSchoolManageAdd() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {userName,password,name,schoolId,type} = r_body
        let json
        if(type===1){
            json = { userName}
        }else if(type===2){
            json = { userName,schoolId}
        }
        const data = await mysql.select('loginManage', {
            where: json
        })
        if(data.length){
            ctx.body = err.err7
        }else{
            await mysql.insert('loginManage', {
                userName,password,name,schoolId,type:type
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: {}
            }
        }
    }
    // 删除校园管理员
    async uSchoolManageDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {manageId} = r_body
        await mysql.delete('loginManage', {
            manageId
        });
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 编辑校园管理员
    async uSchoolManageUpdate() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {password,name,manageId} = r_body
        let params
        if(password){
            params = {
                password,
                name
            }
        }else{
            params = {
                name
            }
        }
        await mysql.update('loginManage',params ,{
            where: {
                manageId
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }

    //  获取校园管理员列表  
    async uSchoolList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.uSchoolList(r_body))
        list.map((item,i)=>{
            item.key=i
        })
        const total = util.handelData(await mysql.query(sqlConfig.uSchoolTotal()))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }
     // 新增校园入驻
    async uSchoolAdd() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {name} = r_body
        const data = await mysql.select('uSchool', {
            where: { name}
        })
        if(data.length){
            ctx.body = err.err8
        }else{
            await mysql.insert('uSchool', {
                name
            })
            const data2 = await mysql.select('uSchool', {
                columns:['id'],
                where: { name}
            })
            const uSchoolId  = data2[0].id
            await mysql.insert('concat', {
                uSchoolId
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: {}
            }
        }
    }
    // 删除校园入驻
    async uSchoolDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id} = r_body
        await mysql.delete('uSchool', {
            id
        });
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 编辑校园入驻
    async uSchoolUpdate() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id,name} = r_body
        let params = {
            name
        }
        await mysql.update('uSchool',params,{
            where: {
                id
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }

    // 新增商铺
    async uBusinessManageAdd() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {manageId,uSchoolId,userName,businessManageId,title,src,uType,tel,address,endTime,businessTime,buyNotice,detail} = r_body
        await mysql.insert('uBusiness', {
            manageId,uSchoolId,userName,businessManageId,src,title,uType,tel,address,endTime,businessTime,buyNotice,detail
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 商铺列表
    async uBusinessManageList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.uBusinessManageList(r_body))
        list.map((item,i)=>{
            item.key=i
        })
        const total = util.handelData(await mysql.query(sqlConfig.uBusinessManageTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }
     // 删除商铺
    async uBusinessManageDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id} = r_body
        await mysql.delete('uBusiness', {
            id
        });
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 编辑商铺
    async uBusinessManageUpdate() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id,title,src,uType,tel,address,endTime,businessTime,buyNotice,detail} = r_body
        let params = {
            title,src,uType,tel,address,endTime,businessTime,buyNotice,detail
        }
        await mysql.update('uBusiness',params,{
            where: {
                id
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 商铺类型列表
    async uTypeList() {
        const { ctx, app } = this;
        const { mysql } = app
        const data = await mysql.select('uType')
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: data
        }
    }
    // 爆款推荐列表
    async uTopList() {
        const { ctx, app } = this;
        const { mysql } = app
        const r_body = ctx.request.body;
        const {uSchoolId} = r_body
        const data = await mysql.select('uTop',{
            where:{
                uSchoolId
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: data
        }
    }
    // 进入爆款区
    async uBusinessEnterTop() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id,topNum} = r_body
        let params = {
            topNum
        }
        await mysql.update('uBusiness',params,{
            where: {
                id
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 新增套餐
    async uTicketAdd() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {uSchoolId,uBusinessId,content,price,priceOld,numTotal,src} = r_body
        await mysql.insert('uTicket', {
            uSchoolId,uBusinessId,content,price,priceOld,numTotal,src
        })
        await mysql.update('uBusiness',{
            isGrounding:1
        },{
            where: {
                id:uBusinessId
            }
        })
        const _data = await mysql.select('uTicket', {
            where: { uBusinessId}
        })
        // 处理uBusiness表的价格和数量
        if(_data.length){
            let [num,numTotal] = [0,0]
            _data.map(item=>{
                num+=item.num,
                numTotal+=item.numTotal
            })
            const _dataNew = util.sortArr(_data,'price')[0]
            const {price,priceOld} = _dataNew
            await mysql.update('uBusiness',{
                isGrounding:1,
                price,
                priceOld,
                num,
                numTotal
            },{
                where: {
                    id:uBusinessId
                }
            })
        }
        
        
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 套餐列表
    async uTicketList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.uTicketList(r_body))
        list.map((item,i)=>{
            item.key=i
        })
        const total = util.handelData(await mysql.query(sqlConfig.uTicketTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }
    // 编辑套餐
    async uTicketUpdate() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id,content,price,priceOld,numTotal,uBusinessId,src} = r_body
        let params = {
            content,price,priceOld,numTotal,src
        }
        await mysql.update('uTicket',params,{
            where: {
                id
            }
        })
        // 处理uBusiness表的价格和数量
        const _data = await mysql.select('uTicket', {
            where: { uBusinessId}
        })
        if(_data.length){
            let [num,numTotal] = [0,0]
            _data.map(item=>{
                num+=item.num,
                numTotal+=item.numTotal
            })
            const _dataNew = util.sortArr(_data,'price')[0]
            const {price,priceOld} = _dataNew
            await mysql.update('uBusiness',{
                isGrounding:1,
                price,
                priceOld,
                num,
                numTotal
            },{
                where: {
                    id:uBusinessId
                }
            })
        }
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 删除套餐
    async uTicketDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id,uBusinessId} = r_body
        await mysql.delete('uTicket', {
            id
        })
        const num = util.handelData(await mysql.query(sqlConfig.uTicketNum(r_body)))[0]['count(*)']
        if(num<1){
            await mysql.update('uBusiness',{
                isGrounding:0
            },{
                where: {
                    id:uBusinessId
                }
            })
        }

        // 处理uBusiness表的价格和数量
        const _data = await mysql.select('uTicket', {
            where: { uBusinessId}
        })
        if(_data.length){
            let [num,numTotal] = [0,0]
            _data.map(item=>{
                num+=item.num,
                numTotal+=item.numTotal
            })
            const _dataNew = util.sortArr(_data,'price')[0]
            const {price,priceOld} = _dataNew
            await mysql.update('uBusiness',{
                isGrounding:1,
                price,
                priceOld,
                num,
                numTotal
            },{
                where: {
                    id:uBusinessId
                }
            })
        }
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }

    // 订单列表
    async uOrderManageList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.uOrderManageList(r_body))
        list.map((item,i)=>{
            item.key=i
        })
        const total = util.handelData(await mysql.query(sqlConfig.uOrderManageTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }

    //  获取爆款区列表  
    async topAreaList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.topAreaList(r_body))
        list.map((item,i)=>{
            item.key=i
        })
        const total = util.handelData(await mysql.query(sqlConfig.topAreaTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }
     // 新增爆款区
    async topAreaAdd() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {name,uSchoolId} = r_body
        const data = await mysql.select('uTop', {
            where: { name,uSchoolId}
        })
        if(data.length){
            ctx.body = err.err9
        }else{
            await mysql.insert('uTop', {
                name,
                uSchoolId
            })
            ctx.body = {
                code: 200,
                msg: 'SUCCESS',
                data: {}
            }
        }
    }
    // 删除爆款区
    async topAreaDelete() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id} = r_body
        await mysql.delete('uTop', {
            id
        });
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }
    // 编辑爆款区
    async topAreaUpdate() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {id,name} = r_body
        let params = {
            name
        }
        await mysql.update('uTop',params,{
            where: {
                id
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {}
        }
    }

    // 商家订单列表
    async uOrderBusList() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.uOrderBusList(r_body))
        list.map((item,i)=>{
            item.key=i
        })
        const total = util.handelData(await mysql.query(sqlConfig.uOrderBusTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }

    // h5验券 查询该商铺用户名（手机号下）下所有 manageId,判断该券是否属于商铺
    async vertifyTicket() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const {userName,manageId,ticketCode} = r_body
        if (r_body) {
            const data = await mysql.select('loginManage',{
                where:{
                    userName,
                    manageId
                }
            })
            if(data.length){
                const result = await mysql.update('uOrder',{
                    status:4
                } ,{
                    where: {
                        ticketCode
                    }
                })
                if(result.affectedRows === 1){
                    ctx.body = {
                        code: 200,
                        msg: 'SUCCESS',
                        data:{}
                    }
                }else{
                    ctx.body = err.err11
                }
                
            }else{
                ctx.body = err.err10
            }
            
        } else {
            ctx.body = err.err1
        }
    }
    
}



module.exports = UBusinessManageController;